package com.central.apps.handler.supplier;

import com.alibaba.fastjson.JSONObject;
import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

//供应商评价
@Component
@Slf4j
public class SupplierEvaluation extends BaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if ("submit".equals(flowNode.getId())){
            List<Map<String, Object>> bySql = handlerService.findBySql("select evaluation from gq_Suppevalua where id =" + id);
            Map<String, Object> map = bySql.get(0);
            String evaluation = MapUtils.getString(map, "evaluation");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select evaluation,finalscore from gq_vendorlist where evaluation ='" + evaluation + "'");
            if (CollectionUtils.isEmpty(bySql1)){
                return "请添加供应商列表相关信息！";
            }else {
                for (int i=0;i<bySql1.size();i++){
                    Map<String, Object> map1 = bySql1.get(i);
                    Float finalscore = MapUtils.getFloat(map1, "finalscore");
                    if (finalscore==0){
                        return "最终得分为0，请计算分数";
                    }
                }
            }
        }
        return super.beforeExcuteFlow(app, id, flowNode, op);
    }

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("SuppevaluaApp".equals(app)){
            if ("getsuppacc".equals(type)){
                JSONObject json = new JSONObject();
                String id = MapUtils.getString(params, "id");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select evaluation from gq_Suppevalua where id='" + id + "'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    Map<String, Object> map = bySql1.get(0);
                    String evaluation = MapUtils.getString(map, "evaluation");
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("SELECT suppliercode from gq_vendorlist where evaluation ='" + evaluation + "'");
                    Map<String, Object> map2 = bySql2.get(0);
                    String suppliercode = MapUtils.getString(map2, "suppliercode");
                    List<Map<String, Object>> bySql = handlerService.findBySql("\tSELECT score from gq_dynamiation where evaluation ='" + evaluation + "'");
                    float num =0;
                    if (CollectionUtils.isNotEmpty(bySql)){
                        for (int i=0;i<bySql.size();i++){
                            Map<String, Object> map1 = bySql.get(i);
                            Float score = MapUtils.getFloat(map1, "score");
                            num = num + score;
                        }

                        if (num <=100 && num >=0){
                            json.put("code","0");
                            handlerService.updateBySql("UPDATE gq_vendorlist set finalscore ="+num+" where evaluation ='"+evaluation+"'");
                            if (num>=90){
                                String grad = "优选";
                                handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                            }else if(num>=70&&num<90){
                                String grad = "可选";
                                handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                            }else  if (num>=60&&num<70){
                                String grad = "慎选";
                                handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                            }else {
                                String grad = "不合格";
                                handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                            }
                            json.put("msg", "计算得分成功!");
                            return json.toString() ;
                        }else {
                            json.put("code","1");
                            json.put("msg", "评定分数不能小于0并且不能超过100!");
                            return json.toString() ;
                        }
                    }
                }
            }
        }
        return super.operate(app, type, params);
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        List<Map<String, Object>> bySql = handlerService.findBySql("select evaluation from gq_suppevalua where id =" + id);
        String evaluation =MapUtils.getString(bySql.get(0),"evaluation");
        List<Map<String, Object>> bySql1 = handlerService.findBySql("select finalscore,gradeafter,suppliercode from gq_vendorlist where evaluation ='" + evaluation + "'");
        if (CollectionUtils.isNotEmpty(bySql1)){
            for (int i=0;i<bySql1.size();i++){
                Map<String, Object> map = bySql1.get(i);
                Float finalscore = MapUtils.getFloat(map, "finalscore");
                String gradeafter = MapUtils.getString(map, "gradeafter");
                String suppliercode = MapUtils.getString(map, "suppliercode");
                handlerService.updateBySql("update gq_account set assessment ='"+finalscore+"',supliergrade='"+gradeafter+"' where supplierno ='"+suppliercode+"'");
            }
        }

    }
}
